Functions with a long parameter list are difficult to use because maintainers must figure out the role of each parameter and keep track of their
position.
fn set_coordinates(x1: i32, y1: i32, z1: i32, x2: i32, y2: i32, z2: i32) { // Noncompliant
// ...
}
The solution can be to:
- Split the function into smaller ones
// Each function does a part of what the original setCoordinates function was doing, so confusion risks are lower
fn set_origin(x: i32, y: i32, z: i32) {
// ...
}
fn set_size(width: i32, height: i32, depth: i32) {
// ...
}
- Find a better data structure for the parameters that group data in a way that makes sense for the specific application domain
struct Point {
x: i32,
y: i32,
}
impl Point {
fn new(x: i32, y: i32) -> Point {
Point { x, y }
}
}
fn set_coordinates(p1: &mut Point, p2: &Point) {
// ...
}
This rule raises an issue when a function has more parameters than the provided threshold.